import { observable, action } from 'mobx';
import { Tools } from 'weaver-mobile';
import { loopCondition } from '../../util';

const initState = {
  step: 0,
  mailState: false,
  results: {},
  bean: {},
  visible: false,
  errorHint: '',
  loading: false,
  isNewInit: true,
  invoiceVisible: false,
};

export default class AddMailAccountStore {
  @observable step = initState.step;
  @observable mailState = initState.mailState;
  @observable results = initState.results;
  @observable visible = initState.visible;
  @observable invoiceVisible = initState.invoiceVisible;
  @observable errorHint = initState.errorHint;
  @observable loading = initState.loading;
  @observable bean = initState.bean;
  @observable isNewInit = initState.isNewInit; // 标志是否新建初始化

  @action
  handleCondition = (condition, step, serverType, isReceiveSent) => {
    if (!condition) return {};
    if (step === undefined) { step = this.step; }
    return loopCondition(condition, (c) => {
      const domkey = c.domkey.join('__');
      const normal = { rules: '', viewAttr: 2 };
      const req = { rules: 'required', viewAttr: 3 };
      const reqStr = { rules: 'required|string', viewAttr: 3 };
      const reqNum = { rules: 'required|numeric', viewAttr: 3 };
      const reqMailAddress = { rules: 'required|email|string', viewAttr: 3 }
      let field = { ...c };
      // accountName, accountMailAddress, accountId, accountPassword - required
      if (['accountName', 'accountId', 'accountPassword'].indexOf(domkey) !== -1) {
        field = { ...field, ...(step === 0 ? req : normal) };
      }
      // accountMailAddress
      if ('accountMailAddress'.indexOf(domkey) !== -1) {
        field = { ...field, ...(step === 0 ? reqMailAddress : normal) };
      }
      // popServer, smtpServer - required|string
      if (['popServer', 'smtpServer'].indexOf(domkey) !== -1) {
        field = { ...field, ...(step === 1 && serverType && (serverType === "1" | serverType === "2") ? reqStr : normal) };
      }
      // sentItems
      if (['sentItems'].indexOf(domkey) !== -1) {
        field = { ...field, ...(step === 1 && serverType && (serverType === "2") && isReceiveSent && (Number(isReceiveSent)) ? reqStr : normal) };
      }
      if (['ews_service_url'].indexOf(domkey) !== -1) {
        field = { ...field, ...(step === 1 && serverType && serverType === "3" ? reqStr : normal) };
      }
      // popServerPort, smtpServerPort - required|numeric
      if (['popServerPort', 'smtpServerPort'].indexOf(domkey) !== -1) {
        field = { ...field, ...(step === 1 ? reqNum : normal) };
      }
      return field;
    });
  }

  @action
  disabledSwitch = (condition, key, v) => {
    if (!condition) return {};
    return loopCondition(condition, (c) => {
      const domkey = c.domkey.join('__');
      let field = { ...c };
      const disabled = { otherParams: { disabled: true }, viewAttr: 1 };
      const normal = { otherParams: {}, viewAttr: 2 };
      // 'sendneedSSL', 'isStartTls'
      if (key === 'sendneedSSL') {
        if (domkey === 'isStartTls') {
          field = { ...field, ...(v ? disabled : normal) };
        }
      } else if (key === 'isStartTls') {
        if (domkey === 'sendneedSSL') {
          field = { ...field, ...(v ? disabled : normal) };
        }
      }
      return field;
    });
  }

  @action
  updateFormFields = (bean, form) => {
    let fields = {};
    Object.keys(bean).forEach(domkey => {
      fields = { ...fields, [domkey]: { value: bean[domkey] } };
    });
    form.updateFields(fields);
  }


  @action
  setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  clear = () => this.setState(initState)

  onGetFolderName = async (params) => {
    let result = await Tools.callApi({ url: '/api/email/folder/getEmailsentFolderName', method: 'GET', params })
    return result
  }
}
